// pages/pyq.js

const randomAvats = [
  "https://s1.ax1x.com/2023/04/18/p9PgreP.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PgBLt.jpg",
  "https://s1.ax1x.com/2023/04/18/p9Pg0sI.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PgwQA.jpg",
  "https://s1.ax1x.com/2023/04/18/p9Pgazd.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PgURH.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PgNJe.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PgtiD.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PgJIO.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PgGdK.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDqWn.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDbJs.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDHij.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDToQ.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDodg.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDIeS.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PD4L8.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDhsf.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDfQP.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDRzt.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PD2RI.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDgJA.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDcid.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDyIH.jpg",
  "https://s1.ax1x.com/2023/04/18/p9PDsde.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJqy9.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJbQJ.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJ7z4.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJTWF.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJoJU.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJIiT.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJ4oV.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJhd0.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJfZq.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJRLn.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJ2ss.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJgMj.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJ6zQ.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJyRg.jpg",
  "https://s1.ax1x.com/2023/04/17/p9CJsJS.jpg",
  "https://s1.ax1x.com/2023/04/16/p9Cne0g.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CnZnS.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CnEX8.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CnA6f.jpg",
  "https://s1.ax1x.com/2023/04/16/p9Cnk1P.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CnFpt.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CnPfI.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CnCtA.jpg",
  "https://s1.ax1x.com/2023/04/16/p9Cn9kd.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CnSTH.jpg",
  "https://s1.ax1x.com/2023/04/16/p9Cmz0e.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CmxmD.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CmjOO.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CmX6K.jpg",
  "https://s1.ax1x.com/2023/04/16/p9CmOl6.jpg",
  "https://s1.ax1x.com/2023/04/16/p9938te.jpg",
  "https://s1.ax1x.com/2023/04/16/p9933kD.jpg",
  "https://s1.ax1x.com/2023/04/16/p993lTO.jpg",
  "https://s1.ax1x.com/2023/04/16/p993Q0K.jpg",
  "https://s1.ax1x.com/2023/04/16/p993Mm6.jpg",
  "https://s1.ax1x.com/2023/04/16/p993uOx.jpg",
  "https://s1.ax1x.com/2023/04/16/p993n61.jpg",
  "https://s1.ax1x.com/2023/04/16/p993mlR.jpg",
  "https://s1.ax1x.com/2023/04/16/p993ep9.jpg",
  "https://s1.ax1x.com/2023/04/16/p993VfJ.jpg",
  "https://s1.ax1x.com/2023/04/16/p993EY4.jpg",
  "https://s1.ax1x.com/2023/04/16/p993AkF.jpg",
  "https://s1.ax1x.com/2023/04/16/p993FTU.jpg",
  "https://s1.ax1x.com/2023/04/16/p993iwT.jpg",
  "https://s1.ax1x.com/2023/04/16/p993PmV.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDQ8f.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDMPP.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDu5t.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDnUI.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDmVA.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDZbd.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDVDH.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDEKe.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDkvD.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDFgO.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDi8K.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDPC6.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SD94x.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SDpU1.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SBOvF.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMKVH.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMnqe.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMmrD.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMeKO.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMVxK.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SME26.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMA8x.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMkP1.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMPa9.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMpb4.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMCVJ.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SKzKU.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SMSrF.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SKj2V.jpg",
  "https://s1.ax1x.com/2023/04/14/p9SKvvT.jpg"
]


Page({
  /**
   * 页面的初始数据
   */
  data: {
    userAvat: "",
    userName: "微信用户",
    pyqText: "朋友圈文本，点击修改",
    pyqTime: "",
    pyqImages: [],
    likeAvats: [],
    // 朋友圈推送图片 + 文案
    articleImage: "../../images/upload.png",
    articleText: "P大2023春第二期征订！T恤福利免费送！",

    // 朋友圈文案是否显示
    showPqyText: true,
    // 图片 | 推送模式
    isArticleMode: true,

    // 朋友圈时间
    curDate: "",
    curTime: "",
    curYear: "",
    curMonth: "",
    curDay: "",

    // 系统参数：
    statusBarHeight: 20,
    platform: "",
    theme: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options)
    this.setData({
      userName: options.userName,
      userAvat: options.userAvat
    })
    // 获取当前时间
    this.setCurTime()
    // 默认 18 个点赞
    this.randomAvat(null, 18)
    // 获取系统信息
    this.getSystemInfo()
  },
  // 获取系统信息：状态栏高度、手机系统
  getSystemInfo() {
    // console.log("getSystemInfo") 
    let that = this
    wx.getSystemInfoAsync({
      success (res) { 
        // 状态栏高度
        // console.log(res.statusBarHeight)
        // 客户端平台：IOS、Android...
        // console.log(res.platform)
        // 系统当前主题：深色、浅色
        // console.log(res.theme)
        that.setData({
          statusBarHeight: res.statusBarHeight,
          platform: res.platform,
          theme: res.theme,
        })
      }
    })
  },
  // 设值当前时间
  setCurTime() {
    // 获取当前时间
    let now = new Date();
    // console.log(now)
    // 格式化时间
    let year = now.getFullYear();
    let month = this.timeFormat(now.getMonth() + 1);
    let day = this.timeFormat(now.getDate());
    let hour = this.timeFormat(now.getHours());
    let minute = this.timeFormat(now.getMinutes());
    this.setData({
      curDate: year + "-" + month + "-" + day,
      curYear: year,
      curMonth: month,
      curDay: day,
      curTime: hour + ":" + minute,
    })
    this.setPyqTime()
  },
  // 时间格式化
  timeFormat(num) {
    if (num < 10) {
      num = `0${num}`;
    }
    // console.log(num)
    return num
  },
  // 朋友圈时间展示
  setPyqTime() {
    let curDate = this.data.curDate
    let curTime = this.data.curTime

    let timeStr = curDate + " " + curTime + ":00";
    let time = new Date(timeStr);
    let now = new Date();
    let isToday = now.getFullYear() === time.getFullYear()
      && now.getMonth() === time.getMonth()
      && now.getDate() === time.getDate();
    if (isToday) {
      this.setData({
        pyqTime: curTime
      })
      return
    }
    
    let yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000);
    let isYesterday = yesterday.getFullYear() === time.getFullYear()
      && yesterday.getMonth() === time.getMonth()
      && yesterday.getDate() === time.getDate();
    if (isYesterday) {
      this.setData({
        pyqTime: "昨天 " + curTime
      })
      return
    }

    let nums = curDate.split("-")
    let year = parseInt(nums[0])
    let month = parseInt(nums[1])
    let day = parseInt(nums[2])
    // console.log(year + " " + month + " " + day)
    this.setData({
      pyqTime: year + "年" + month + "月" + day + "日 " + curTime
    })
    
  },
  // 日期选择器
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      curDate: e.detail.value
    })
    this.setPyqTime()
  },
  // 时间选择器
  bindTimeChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      curTime: e.detail.value
    })
    this.setPyqTime()
  },
  // 修改朋友圈文案
  pyqTextInput(e) {
    this.setData({
      pyqText: e.detail.value
    })
  },
  // 朋友圈图片
  pyqImageUpload(e) {
    var that = this;
    
    wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        // console.log(res.tempFiles.tempFilePath)
        // console.log(res.tempFiles.size)
        let tempPyqImages = res.tempFiles
        let len = tempPyqImages.length
        // 4 张 = 2 * 2
        if (len == 4) {
          // 第 3 张
          tempPyqImages.splice(2, 0, "../../images/blank.png")
          // 第 6 张
          tempPyqImages.push("../../images/blank.png")
        }
        // 不足行补足行（如 5 张、8 张）
        else if (len % 3 != 0) {
          let n = 3 - (len % 3);
          for (let i = 0; i < n; i++) {
            tempPyqImages.push("../../images/blank.png")
          }
        }
        that.setData({
          pyqImages: tempPyqImages
        })
        // console.log(that.data.pyqImages)
      }
    })
  },
  // 修改推送文案
  articleTextInput(e) {
    this.setData({
      articleText: e.detail.value
    })
  },
  // 推送图片
  articleImageUpload(e) {
    // console.log(e)
    var that = this;
    
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album'],
      success(res) {
        // console.log(res)
        that.setData({
          articleImage: res.tempFiles[0].tempFilePath
        })
        // console.log(that.data.pyqImages)
      }
    })
  },
  // 修改朋友圈文案是否可见
  changePyqTextStatus() {
    let isShow = this.data.showPqyText
    this.setData({
      showPqyText: !isShow,
    })
  },
  // 修改 图片 | 推送 模式
  changePyqImageStatus() {
    let otherMode = this.data.isArticleMode
    this.setData({
      isArticleMode: !otherMode,
    })
  },
  // 确定点赞人数
  randomAvat(e, number) {
    let num
    if (e != null) {
      // console.log(e)
      num = +e.detail.value
    } else {
      num = number
    }

    // console.log(num)
    if (isNaN(num) || num < 0 || num > 99) {
      wx.showToast({
        title: '请输入 0~99',
      })
      return
    }

    let limit = randomAvats.length
    let tempRandomAvats = []
    // console.log("limit " + limit)
    // console.log("tempRandomAvats " + tempRandomAvats)
    for (let i = 0; i < num;) {
      let randomIndex = this.randomInteger(0, limit - 1)
      // console.log(i, randomIndex)
      if (tempRandomAvats.indexOf(randomAvats[randomIndex]) == -1) {
        tempRandomAvats.push(randomAvats[randomIndex])
        i++
      }
    } 
    // console.log("tempRandomAvats " + tempRandomAvats)
    this.setData({
      likeAvats: tempRandomAvats
    })
  },
  // 获取随机整数
  randomInteger(min, max) {
    let random = min + Math.random() * (max + 1 - min);
    return Math.floor(random);
  },
  // 生成截图 todo
  generatorScreenshot(e) {
    wx.showToast({
      title: '请自行截图',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      articleText: this.data.articleText
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})